package com.situ.student.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.student.dao.IStudentDao;
import com.situ.student.entity.Student;
import com.situ.student.util.JDBCUtil;

/**
 * 用JDBC的方式实现数据的增删改查
 */
public class StudentDaoJdbcImpl implements IStudentDao {
	/**
	 * 插入学生
	 */
	@Override
	public int insert(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(name,age,gender) values(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getAge());
			preparedStatement.setString(3, student.getGender());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * list集合显示出所有学生信息
	 */
	@Override
	public List<Student> list() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name,age,gender FROM student";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}

		return list;
	}

	/**
	 * 通过id删除学生
	 */
	@Override
	public int deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 通过姓名查找学生
	 */
	@Override
	public List<Student> findByName(String name) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select * from student where name=?";
			// String sql = "SELECT id,name,age,gender FROM student";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, name);
			// 查询(没有改变表里面数据)：select executeQuery() 返回结果集：ResultSet
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				if (name.equals(resultSet.getString("name"))) {
					Integer id = resultSet.getInt("id");
					String fbname = resultSet.getString("name");
					Integer age = resultSet.getInt("age");
					String gender = resultSet.getString("gender");
					Student student = new Student(id, fbname, age, gender);
					list.add(student);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	/**
	 * 修改学生
	 */
	@Override
	public int updateStudent(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			String sql = "update student set name = ?, age = ?,gender = ? where id= ?";
			connection = JDBCUtil.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			// 设置SQL语句的值
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getAge());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getId());
			// 处理结果
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("修改成功");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

}
